package system

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/system"
	systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type Seckill_activity_deviceApi struct{}

// CreateSeckill_activity_device 创建秒杀活动设备
// @Tags Seckill_activity_device
// @Summary 创建秒杀活动设备
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body system.Seckill_activity_device true "创建秒杀活动设备"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /seckill_activity_device/createSeckill_activity_device [post]
func (seckill_activity_deviceApi *Seckill_activity_deviceApi) CreateSeckill_activity_device(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	var seckill_activity_device system.Seckill_activity_device
	err := c.ShouldBindJSON(&seckill_activity_device)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = seckill_activity_deviceService.CreateSeckill_activity_device(ctx, &seckill_activity_device)
	if err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("创建成功", c)
}

// DeleteSeckill_activity_device 删除秒杀活动设备
// @Tags Seckill_activity_device
// @Summary 删除秒杀活动设备
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body system.Seckill_activity_device true "删除秒杀活动设备"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /seckill_activity_device/deleteSeckill_activity_device [delete]
func (seckill_activity_deviceApi *Seckill_activity_deviceApi) DeleteSeckill_activity_device(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	ID := c.Query("ID")
	err := seckill_activity_deviceService.DeleteSeckill_activity_device(ctx, ID)
	if err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

// DeleteSeckill_activity_deviceByIds 批量删除秒杀活动设备
// @Tags Seckill_activity_device
// @Summary 批量删除秒杀活动设备
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
// @Router /seckill_activity_device/deleteSeckill_activity_deviceByIds [delete]
func (seckill_activity_deviceApi *Seckill_activity_deviceApi) DeleteSeckill_activity_deviceByIds(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	IDs := c.QueryArray("IDs[]")
	err := seckill_activity_deviceService.DeleteSeckill_activity_deviceByIds(ctx, IDs)
	if err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("批量删除成功", c)
}

// UpdateSeckill_activity_device 更新秒杀活动设备
// @Tags Seckill_activity_device
// @Summary 更新秒杀活动设备
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body system.Seckill_activity_device true "更新秒杀活动设备"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /seckill_activity_device/updateSeckill_activity_device [put]
func (seckill_activity_deviceApi *Seckill_activity_deviceApi) UpdateSeckill_activity_device(c *gin.Context) {
	// 从ctx获取标准context进行业务行为
	ctx := c.Request.Context()

	var seckill_activity_device system.Seckill_activity_device
	err := c.ShouldBindJSON(&seckill_activity_device)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = seckill_activity_deviceService.UpdateSeckill_activity_device(ctx, seckill_activity_device)
	if err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("更新成功", c)
}

// FindSeckill_activity_device 用id查询秒杀活动设备
// @Tags Seckill_activity_device
// @Summary 用id查询秒杀活动设备
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param ID query uint true "用id查询秒杀活动设备"
// @Success 200 {object} response.Response{data=system.Seckill_activity_device,msg=string} "查询成功"
// @Router /seckill_activity_device/findSeckill_activity_device [get]
func (seckill_activity_deviceApi *Seckill_activity_deviceApi) FindSeckill_activity_device(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	ID := c.Query("ID")
	reseckill_activity_device, err := seckill_activity_deviceService.GetSeckill_activity_device(ctx, ID)
	if err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败:"+err.Error(), c)
		return
	}
	response.OkWithData(reseckill_activity_device, c)
}

// GetSeckill_activity_deviceList 分页获取秒杀活动设备列表
// @Tags Seckill_activity_device
// @Summary 分页获取秒杀活动设备列表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data query systemReq.Seckill_activity_deviceSearch true "分页获取秒杀活动设备列表"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /seckill_activity_device/getSeckill_activity_deviceList [get]
func (seckill_activity_deviceApi *Seckill_activity_deviceApi) GetSeckill_activity_deviceList(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	var pageInfo systemReq.Seckill_activity_deviceSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := seckill_activity_deviceService.GetSeckill_activity_deviceInfoList(ctx, pageInfo)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败:"+err.Error(), c)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}

// GetSeckill_activity_devicePublic 不需要鉴权的秒杀活动设备接口
// @Tags Seckill_activity_device
// @Summary 不需要鉴权的秒杀活动设备接口
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=object,msg=string} "获取成功"
// @Router /seckill_activity_device/getSeckill_activity_devicePublic [get]
func (seckill_activity_deviceApi *Seckill_activity_deviceApi) GetSeckill_activity_devicePublic(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	// 此接口不需要鉴权
	// 示例为返回了一个固定的消息接口，一般本接口用于C端服务，需要自己实现业务逻辑
	seckill_activity_deviceService.GetSeckill_activity_devicePublic(ctx)
	response.OkWithDetailed(gin.H{
		"info": "不需要鉴权的秒杀活动设备接口信息",
	}, "获取成功", c)
}

// UpdateStockSaga 发起Saga，前端编辑库存时用
// @Tags Seckill_activity_device
// @Summary 发起Saga，前端编辑库存时用
// @Accept application/json
// @Produce application/json
// @Param data body systemReq.UpdateStockSagaRequest true "库存变更请求"
// @Success 200 {object} response.Response{msg=string} "成功"
// @Router /seckill_activity_device/updateStockSaga [POST]
func (seckill_activity_deviceApi *Seckill_activity_deviceApi) UpdateStockSaga(c *gin.Context) {
	ctx := c.Request.Context()

	var req systemReq.UpdateStockSagaRequest
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	err = seckill_activity_deviceService.UpdateStockSaga(ctx, req)
	if err != nil {
		global.GVA_LOG.Error("库存变更失败!", zap.Error(err))
		response.FailWithMessage("库存变更失败: "+err.Error(), c)
		return
	}
	response.OkWithMessage("库存变更成功", c)
}

// DecreaseStock Saga步骤1，扣减真实库存
// @Tags Seckill_activity_device
// @Summary Saga步骤1，扣减真实库存
// @Accept application/json
// @Produce application/json
// @Param data body systemReq.DecreaseStockRequest true "扣减库存请求"
// @Success 200 {object} response.Response{msg=string} "成功"
// @Router /seckill_activity_device/decreaseStock [POST]
func (seckill_activity_deviceApi *Seckill_activity_deviceApi) DecreaseStock(c *gin.Context) {
	ctx := c.Request.Context()

	var req systemReq.DecreaseStockRequest
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	err = seckill_activity_deviceService.DecreaseStock(ctx, req)
	if err != nil {
		global.GVA_LOG.Error("扣减库存失败!", zap.Error(err))
		response.FailWithMessage("扣减库存失败: "+err.Error(), c)
		return
	}
	response.OkWithMessage("扣减库存成功", c)
}

// DecreaseStockCompensate Saga步骤1补偿
// @Tags Seckill_activity_device
// @Summary Saga步骤1补偿
// @Accept application/json
// @Produce application/json
// @Param data body systemReq.DecreaseStockRequest true "补偿请求"
// @Success 200 {object} response.Response{msg=string} "成功"
// @Router /seckill_activity_device/decreaseStockCompensate [POST]
func (seckill_activity_deviceApi *Seckill_activity_deviceApi) DecreaseStockCompensate(c *gin.Context) {
	ctx := c.Request.Context()

	var req systemReq.DecreaseStockRequest
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	err = seckill_activity_deviceService.DecreaseStockCompensate(ctx, req)
	if err != nil {
		global.GVA_LOG.Error("补偿失败!", zap.Error(err))
		response.FailWithMessage("补偿失败: "+err.Error(), c)
		return
	}
	response.OkWithMessage("补偿成功", c)
}

// UpdateStock Saga步骤2，更新活动库存
// @Tags Seckill_activity_device
// @Summary Saga步骤2，更新活动库存
// @Accept application/json
// @Produce application/json
// @Param data body systemReq.UpdateStockRequest true "更新库存请求"
// @Success 200 {object} response.Response{msg=string} "成功"
// @Router /seckill_activity_device/updateStock [POST]
func (seckill_activity_deviceApi *Seckill_activity_deviceApi) UpdateStock(c *gin.Context) {
	ctx := c.Request.Context()

	var req systemReq.UpdateStockRequest
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	err = seckill_activity_deviceService.UpdateStock(ctx, req)
	if err != nil {
		global.GVA_LOG.Error("更新库存失败!", zap.Error(err))
		response.FailWithMessage("更新库存失败: "+err.Error(), c)
		return
	}
	response.OkWithMessage("更新库存成功", c)
}

// UpdateStockCompensate Saga步骤2补偿
// @Tags Seckill_activity_device
// @Summary Saga步骤2补偿
// @Accept application/json
// @Produce application/json
// @Param data body systemReq.UpdateStockRequest true "补偿请求"
// @Success 200 {object} response.Response{msg=string} "成功"
// @Router /seckill_activity_device/updateStockCompensate [POST]
func (seckill_activity_deviceApi *Seckill_activity_deviceApi) UpdateStockCompensate(c *gin.Context) {
	ctx := c.Request.Context()

	var req systemReq.UpdateStockRequest
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	err = seckill_activity_deviceService.UpdateStockCompensate(ctx, req)
	if err != nil {
		global.GVA_LOG.Error("补偿失败!", zap.Error(err))
		response.FailWithMessage("补偿失败: "+err.Error(), c)
		return
	}
	response.OkWithMessage("补偿成功", c)
}

// PreheatStock 前端"预热"按钮用
// @Tags Seckill_activity_device
// @Summary 前端"预热"按钮用
// @Accept application/json
// @Produce application/json
// @Param data body systemReq.PreheatStockRequest true "预热请求"
// @Success 200 {object} response.Response{msg=string} "成功"
// @Router /seckill_activity_device/preheatStock [POST]
func (seckill_activity_deviceApi *Seckill_activity_deviceApi) PreheatStock(c *gin.Context) {
	ctx := c.Request.Context()

	var req systemReq.PreheatStockRequest
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	err = seckill_activity_deviceService.PreheatStock(ctx, req)
	if err != nil {
		global.GVA_LOG.Error("预热失败!", zap.Error(err))
		response.FailWithMessage("预热失败: "+err.Error(), c)
		return
	}
	response.OkWithMessage("预热成功", c)
}

// ClearRedis 补偿或手动清空Redis
// @Tags Seckill_activity_device
// @Summary 补偿或手动清空Redis
// @Accept application/json
// @Produce application/json
// @Param data body systemReq.ClearRedisRequest true "清空Redis请求"
// @Success 200 {object} response.Response{msg=string} "成功"
// @Router /seckill_activity_device/clearRedis [POST]
func (seckill_activity_deviceApi *Seckill_activity_deviceApi) ClearRedis(c *gin.Context) {
	ctx := c.Request.Context()

	var req systemReq.ClearRedisRequest
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	err = seckill_activity_deviceService.ClearRedis(ctx, req)
	if err != nil {
		global.GVA_LOG.Error("清空Redis失败!", zap.Error(err))
		response.FailWithMessage("清空Redis失败: "+err.Error(), c)
		return
	}
	response.OkWithMessage("清空Redis成功", c)
}

// CacheAllToRedis 缓存列表
// @Tags Seckill_activity_device
// @Summary 缓存列表
// @Accept application/json
// @Produce application/json
// @Param data query systemReq.Seckill_activity_deviceSearch true "成功"
// @Success 200 {object} response.Response{data=object,msg=string} "成功"
// @Router /seckill_activity_device/cacheAllToRedis [POST]
func (seckill_activity_deviceApi *Seckill_activity_deviceApi) CacheAllToRedis(c *gin.Context) {
	ctx := c.Request.Context()
	err := seckill_activity_deviceService.CacheAllToRedis(ctx)
	if err != nil {
		global.GVA_LOG.Error("刷新缓存失败!", zap.Error(err))
		response.FailWithMessage("刷新缓存失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("刷新缓存成功", c)
}

// GetAllFromRedis 获取缓存列表数据
// @Tags Seckill_activity_device
// @Summary 获取缓存列表数据
// @Accept application/json
// @Produce application/json
// @Param data query systemReq.Seckill_activity_deviceSearch true "成功"
// @Success 200 {object} response.Response{data=object,msg=string} "成功"
// @Router /seckill_activity_device/getAllFromRedis [GET]
func (seckill_activity_deviceApi *Seckill_activity_deviceApi) GetAllFromRedis(c *gin.Context) {
	ctx := c.Request.Context()
	list, err := seckill_activity_deviceService.GetAllFromRedis(ctx)
	if err != nil {
		global.GVA_LOG.Error("获取缓存失败!", zap.Error(err))
		response.FailWithMessage("获取缓存失败:"+err.Error(), c)
		return
	}
	response.OkWithData(list, c)
}

// 秒杀下单请求体
type SeckillOrderRequest struct {
	ActivityDeviceID int    `json:"activity_device_id" binding:"required"`
	Address          string `json:"address" binding:"required"`
	PayType          string `json:"pay_type" binding:"required"`
}

// @Tags SeckillOrder
// @Summary 秒杀下单
// @Accept application/json
// @Produce application/json
// @Param data body SeckillOrderRequest true "下单参数"
// @Success 200 {object} response.Response{msg=string} "下单成功"
// @Router /seckill_order/seckill [post]
func (api *Seckill_activity_deviceApi) SeckillCreateOrder(c *gin.Context) {
	var req SeckillOrderRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	// 直接写死 userID = 1
	userID := 1
	err := seckillOrderService.SeckillCreateOrder(c.Request.Context(), userID, req.ActivityDeviceID, req.Address, req.PayType)
	if err != nil {
		global.GVA_LOG.Error("下单失败!", zap.Error(err))
		response.FailWithMessage("下单失败: "+err.Error(), c)
		return
	}
	response.OkWithMessage("下单成功", c)
}
